package pl.streamsoft.currencyApp.GUI;

import javax.swing.JOptionPane;

import pl.streamsoft.currencyApp.GUI.services.AppService;
import pl.streamsoft.currencyApp.GUI.services.IAppService;

public class CurrencyAppTest {

	/**
	 * @param args
	 * fdsfds
	 * fggdgf133333333344444
	 */
	public static void main(String[] args) {
		try {
			IAppService appService = new AppService();
			int choice = 1;
			while(choice!=0){
				choice = Integer.parseInt(JOptionPane.showInputDialog(
						"Welcome to Currency Application choose menu item: \n" 
						+"1. NIEMA!!!!   Show all current currencies.\n"
						+"2. Show currencies from date.\n"
						+"3. Add country.\n"
						+"4. Add existing currency into existing country by entering currency code and country name.\n"
						+"5. Add new currency into existing country by entering currency info and country name.\n"
						+"6. Show all currencies of the existing country.\n"
						+"7. Show all currencies from interval.\n"
						+"8. Show all currencies where name matches word.\n"
						+"0. Exit"
						));
				switch(choice){
					case 1:
						System.out.println(appService.showAllCurrentCurrencies());
						break;
					case 2:
						 String answer = JOptionPane.showInputDialog("Please choose right correct menu item: \n"+
								 					 "a - table of average foreign currency; \n"+
												     "b - a table of average non-convertible currencies; \n"+
												     "c - table bid and offer; \n"+
												     "h - EUA rate table.");
						 if(answer.length()==1 && (answer.equals("a") || answer.equals("b") || answer.equals("c") || answer.equals("h")))
						 {
							 String date = JOptionPane.showInputDialog("Enter the date like yymmdd:");
							 if(date.length()!=6){
								 System.out.println("Wrong date!");
							 }
							 else{
								 System.out.println(appService.showAllCurrenciesFromDateByType(date,answer.toCharArray()[0]));
							 }
						 }
						 else{
							 System.out.println("You choose wrong menu item!");
						 }
						
						break;
					case 3:
						String nameOfTheCountry2 = JOptionPane.showInputDialog("Enter the name of country: ");
						boolean result2 = appService.addCountry(nameOfTheCountry2);
						if(result2){
							System.out.println("The new country '"+nameOfTheCountry2+"' was added.");
							System.out.println("----------------------------------------------------------");
						}
						else{
							System.out.println("You enter wrong data!");
							System.out.println("----------------------------------------------------------");
						}
						break;
					case 4:
						String currencyCode3 = JOptionPane.showInputDialog("Enter the code of existing currency: ");
						String countryName3 = JOptionPane.showInputDialog("Enter the name of existing country: ");
						boolean result3 = appService.addExistingCurrencyToCountryByCode(currencyCode3, countryName3);
						if(result3){
							System.out.println("The existing currency with code '"+currencyCode3+"' was added into country '"+countryName3+"'.");
							System.out.println("----------------------------------------------------------");
						}
						else{
							System.out.println("You enter wrong data!");
							System.out.println("----------------------------------------------------------");
						}
						break;
					case 5:
						try{
							String countryName4 = JOptionPane.showInputDialog("Enter the name of existing country: ");
							boolean res4 = appService.getCountry(countryName4);
							if(res4){
								String currencyName4 = JOptionPane.showInputDialog("Enter currency name: ");
								String currencyCode4 = JOptionPane.showInputDialog("Enter currency code: ");
								int currencyConverter4 = Integer.parseInt(JOptionPane.showInputDialog("Enter currency converter: "));
								String averageRate4 = JOptionPane.showInputDialog("Enter currency average rate: ");
								boolean result4 = appService.addNewCurrencyToCountry(countryName4, currencyName4, currencyCode4, currencyConverter4, averageRate4);
								if(result4){
									System.out.println("The new currency was added into existing country '"+countryName4+"'.");
									System.out.println("----------------------------------------------------------");
								}
								else{
									System.out.println("You enter wrong data!");
									System.out.println("----------------------------------------------------------");
								}
							}
							else{
								System.out.println("You enter wrong data!");
								System.out.println("----------------------------------------------------------");
							}
						}
						catch(Exception e){
							System.out.println("You enter wrong data!");
							System.out.println("----------------------------------------------------------");
						}
						break;
					case 6:
						String nameOfExistingCountry5 = JOptionPane.showInputDialog("Enter name of existing country: ");
						String result5 = appService.showAllCurrenciesOfCountry(nameOfExistingCountry5);
						if(!result5.isEmpty()){
							System.out.println("Existing currencies of the country '"+nameOfExistingCountry5+"' are: ");
							System.out.println(result5);
						}
						else{
							System.out.println("Country doesn't exist or you enter wrong data!");
							System.out.println("----------------------------------------------------------");
						}
						break;
					case 7:
						//powinno byc pobierane nie z bazy a z serwisu
						String from = JOptionPane.showInputDialog("Starts from:");
						String to = JOptionPane.showInputDialog("to:");
						String result = appService.showAllCurrenciesFromInterval(from, to);
						if(result.length()<=1){
							System.out.println("You enter wrong data!");
						}
						else{
							System.out.println(result);
						}
						break;
					case 8:
						//powinno byc pobierane nie z bazy a z serwisu
						String word = JOptionPane.showInputDialog("Please enter some word:");
						System.out.println(appService.showAllCurrenciesMatchesWord(word));
						break;
					case 0:
						choice = 0;
						break;
					default:
						System.out.println("Please choose the correct option of menu!");
						System.out.println("----------------------------------------------------------");
						break;
				}
			}
			
			/*CurrencyService curService =
					new CurrencyService(new InputStreamReader(new URL("http://nbp.pl/kursy/xml/LastA.xml").openStream(), "ISO-8859-2"), new XMLCurrencyParser());
			
			List<Currency> lista = curService.getListOfCurrencies();
			
			for(Currency el: lista){
				System.out.println("Name of the currency: "+el.getName()+ ", code: "+el.getCurrencyCode()+ ", converter: "+ el.getConverter()+ ", average rate: "+el.getAverageRate());
			}
			
			Country countryPoland = new Country();
			countryPoland.setName("POLAND");
			List<Currency> listOfPolandCurrencies = new ArrayList<Currency>();
			listOfPolandCurrencies.add(curService.getCurrencyByName("bat (Tajlandia)"));
			listOfPolandCurrencies.add(curService.getCurrencyByCode("USD"));
			for(Currency el: listOfPolandCurrencies){
				countryPoland.getCurrencyList().add(el);
			}
			
			Country countryUkraine = new Country();
			countryUkraine.setName("UKRAINE");
			List<Currency> listOfUkraineCurrencies = new ArrayList<Currency>();
			listOfUkraineCurrencies.add(curService.getCurrencyByName("dolar amerykański"));
			listOfUkraineCurrencies.add(curService.getCurrencyByCode("HKD"));
			for(Currency el: listOfUkraineCurrencies){
				countryUkraine.getCurrencyList().add(el);
			}
			
			List<Country> listOfCountries = new ArrayList<Country>();
			listOfCountries.add(countryPoland);
			listOfCountries.add(countryUkraine);
			
			ICountryService countryService = new CountryService(listOfCountries);
			Country countryFrance = new Country();
			countryFrance.setName("FRANCE");
			List<Currency> listOfFranceCurrency = new ArrayList<Currency>();
			listOfFranceCurrency.add(curService.getCurrencyByName("funt szterling"));
			listOfFranceCurrency.add(curService.getCurrencyByCode("EUR"));
			for(Currency el: listOfFranceCurrency){
				countryFrance.getCurrencyList().add(el);
			}
			countryService.addCountryToList(countryFrance);
			
			System.out.println("--------------------------------------------------------------------------------------------------");
			
			Country countryP = countryService.getCountryByName("POLAND");
			System.out.println("POLISH CURRENCIES ARE:");
			for(Currency el: countryP.getCurrencyList()){
				System.out.println("Name of the currency: "+el.getName()+ ", code: "+el.getCurrencyCode()+ ", converter: "+ el.getConverter()+ ", average rate: "+el.getAverageRate());
			}
			
			System.out.println("--------------------------------------------------------------------------------------------------");
			
			List<Country> listOfC = countryService.getListOfCountries();
			for(Country el: listOfC){
				System.out.println("Name of country is: "+el.getName());
				for(Currency elem: el.getCurrencyList()){
					System.out.println("Name of the currency: "+elem.getName()+ ", code: "+elem.getCurrencyCode()+ ", converter: "+ elem.getConverter()+ ", average rate: "+elem.getAverageRate());
				}
			}*/
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
